package com.front.web.lvyouFood;

import com.bootdo.common.domain.Tree;
import com.bootdo.common.utils.BuildTree;
import com.bootdo.common.utils.GPSUtil;
import com.bootdo.common.utils.R;
import com.bootdo.common.utils.StringUtils;
import com.bootdo.lvyou.controller.LvyouCateImgController;
import com.bootdo.lvyou.domain.*;
import org.springframework.web.HttpRequestHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/f/lvyou/FoodShopController/")
public class FoodShopController extends BaseFoodShopController{


    /**
     * 获取美食分类
     *
     *              http://localhost:8090/f/lvyou/FoodShopController/getClassify
     * @return
     */
    @RequestMapping("getClassify")
    public R getClassify(){
        HashMap<String, Object> param = new HashMap<>();
        param.put("isShow",1);
        List<LvyouCateClassify> list = lvyouCateClassifyService.list(param);

        return R.ok().put("l",list);
    }


    /**
     * 获取当前城市的所有美食商家
     *                   http://localhost:8090/f/lvyou/FoodShopController/getCityAllFoodShop?cityId=&area=&startPrice=&endPrice=&classifyId=&element=&order=&pageNo=&pageSize=
     *
     * @return
     */
    @RequestMapping("getCityAllFoodShop")
    public R getCityAllFoodShop(@RequestParam(value = "cityId",defaultValue = "")Long cityId,//城市id  如邯郸的id
                                @RequestParam(value = "lng",defaultValue = "")Double lng,//城市id  如邯郸的id
                                @RequestParam(value = "lat",defaultValue = "")Double lat,//城市id  如邯郸的id
                                @RequestParam(value = "area",defaultValue = "")Long area,//区域id  如武安或者永年的id
                                @RequestParam(value = "startPrice",defaultValue = "")Double startPrice,//起始价格
                                @RequestParam(value = "endPrice",defaultValue = "")Double endPrice,//结束价格
                                @RequestParam(value = "classifyId",defaultValue = "")Long classifyId,//美食分类的 id   如 川菜的id
                                @RequestParam(value = "element",defaultValue = "")String element ,//筛选查询  如按价格从高到低  distance按照距离 pre价格  配合着order 一起使用
                                @RequestParam(value = "order",defaultValue = "asc")String order,//正序 倒序 desc asc
                                @RequestParam(value = "pageNo",defaultValue = "1")Integer pageNo,//分页页数
                                @RequestParam(value = "pageSize",defaultValue = "100")Integer pageSize,//分页条数
                                @RequestParam(value = "search",defaultValue = "")String search,//search  搜索
                                @RequestParam(value = "isHot",defaultValue = "")String isHot,//是否热门  搜索
                                HttpServletRequest request, HttpServletResponse response){


        if(null == lng || null == lat){
            return R.error("请传入位置信息");
        }

        //如果不是测试调用  就转gps 偏移
        if(StringUtils.isEmpty(request.getParameter("b"))){
            //火星转百度09
            double[] gps =  GPSUtil.gcj02_To_Bd09(lat, lng);
            lat = gps[0];
            lng = gps[1];
        }

        /*if(StringUtils.isEmpty(element)){
            element = "distance";//位置排序
        }*/

        HashMap<String, Object> param = new HashMap<>();
        param.put("provincialId",cityId);//全城
        param.put("cityId",area);//区域
        param.put("classifyId",classifyId);//分类id
        param.put("endPrice",endPrice);//结束价格
        param.put("startPrice",startPrice);//开始价格
        param.put("classifyId",classifyId);//分类查询
        param.put("element",element);//筛选查询
        param.put("order",order);//正序倒序
        param.put("isShow",1);//显示
        param.put("offset",(pageNo-1)*pageSize);//分页
        param.put("limit",pageSize);//分页
        param.put("keyword",search);//搜索关键字
        param.put("name",search);//搜索店名
        param.put("lon",lng);//经纬度
        param.put("lat",lat);//搜索店名
        param.put("isHot",isHot);//搜索热门
        param.put("classifyName",search);//搜索


        List<LvyouCate> list = lvyouCateService.listByRough(param);
        List<LvyouCate> newList = new ArrayList<>();
        for(LvyouCate lvyouCate:list){
            if(lvyouCate.getDistance()!=null&&lvyouCate.getDistance()<30){
                Double lat1 = lvyouCate.getLat();
                Double lng1 = lvyouCate.getLng();

                double[] gps =  GPSUtil.bd09_To_Gcj02(lat1, lng1);
                lat1 = gps[0];
                lng1 = gps[1];
                lvyouCate.setLat(lat1);
                lvyouCate.setLng(lng1);
                newList.add(lvyouCate);
            }

        }
        return R.ok().put("l",newList);
    }

    /**
     * 获取商家详细信息      http://localhost:8090/f/lvyou/FoodShopController/getFoodShopId?id=
     * @return
     */
    @RequestMapping("getFoodShopId")
    public R getFoodShopId(@RequestParam(value = "id",defaultValue = "")Long id){//商家id

        List<LvyouCateImg> carouselList=lvyouCateImgService.getByIdAll(id, LvyouCateImgController.CATELOGO);//获取轮播图
        List<LvyouCateImg> dishesList=lvyouCateImgService.getByIdAll(id, LvyouCateImgController.CAIPINLOGO);//获取推荐菜品图
        LvyouCate lvyouCate = lvyouCateService.get(id);
        Double lat = lvyouCate.getLat();
        Double lng = lvyouCate.getLng();
        double[] gps =  GPSUtil.bd09_To_Gcj02(lat, lng);
        lat = gps[0];
        lng = gps[1];
        lvyouCate.setLat(lat);
        lvyouCate.setLng(lng);
        try {
            lvyouCate.setCarouselImg(carouselList);
            lvyouCate.setGreens(dishesList);
        }catch (Exception e){
            lvyouCate.setCarouselImg(null);
            lvyouCate.setGreens(null);
        }
        return R.ok().put("l",lvyouCate);
    }


    /**
     * 根据cityId  获取该城市下的所有非遗文化
     *                                          http://localhost:8090/f/lvyou/FoodShopController/getFeiYiByCityId?cityId=
     * @return
     */
    @RequestMapping("getFeiYiByCityId")
    public R getFeiYiByCityId(@RequestParam(value = "cityId",defaultValue = "")Long cityId,
                              @RequestParam(value = "lng",defaultValue = "")Double lng,
                                @RequestParam(value = "lat",defaultValue = "")Double lat){//城市id

        if(cityId!=null){
            List<LvyouFeiyi> realList = lvyouFeiyiService.getByCityId(cityId);
            for(LvyouFeiyi feiyi:realList){

                Double lat1 = feiyi.getLat();
                Double lng1 = feiyi.getLng();

                double[] gps =  GPSUtil.bd09_To_Gcj02(lat1, lng1);
                lat1 = gps[0];
                lng1 = gps[1];
                feiyi.setLat(lat1);
                feiyi.setLng(lng1);
            }
            return R.ok().put("l",realList);
        }else{
            HashMap<String, Object> param = new HashMap<>();
            param.put("lat",lat);
            param.put("lng",lng);
            param.put("offset",0);
            param.put("limit",10);
            param.put("isShow","1");
            List<LvyouFeiyi>  list=lvyouFeiyiService.getByCityIdLatAndLng(param);
            if(list.size()==0||list==null){
               return R.error("暂无数据");
            }

            LvyouFeiyi lvyouFeiyi = list.get(0);
            Long cityId1 = lvyouFeiyi.getCityId();
            List<LvyouFeiyi> realList = lvyouFeiyiService.getByCityId(cityId1);

            for(LvyouFeiyi feiyi:realList){

                Double lat1 = feiyi.getLat();
                Double lng1 = feiyi.getLng();

                double[] gps =  GPSUtil.bd09_To_Gcj02(lat1, lng1);
                lat1 = gps[0];
                lng1 = gps[1];
                feiyi.setLat(lat1);
                feiyi.setLng(lng1);
            }
            return R.ok().put("l",realList);
        }

    }


    /**
     * 获取非遗详细情况
     * @param feiyiId
     * @return
     */
    @RequestMapping("getFeiyiInfo")
    public R getFeiyiInfo(@RequestParam(value = "feiyiId",defaultValue = "")Long feiyiId){

        LvyouFeiyi feiyi = lvyouFeiyiService.get(feiyiId);
        Double lat1 = feiyi.getLat();
        Double lng1 = feiyi.getLng();

        double[] gps =  GPSUtil.bd09_To_Gcj02(lat1, lng1);
        lat1 = gps[0];
        lng1 = gps[1];
        feiyi.setLat(lat1);
        feiyi.setLng(lng1);

        return R.ok().put("l",feiyi);

    }
    /**
     * 根据cityId  获取区\县
     *                  http://localhost:8090/f/lvyou/FoodShopController/getCity?cityId=
     * @return
     */
    @RequestMapping("getCity")
    public R getCity(){
        HashMap<String, Object> param = new HashMap<>();

        List<LvyouCity> list = lvyouCityService.getReTopByAll();
        return R.ok().put("l",list);
    }

}
